package org.jeecg.modules.operations.assistant.vehicle.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.operations.assistant.vehicle.util.ApiServiceUtil;
import org.jeecg.modules.operations.assistant.work.entity.CustomerInfo;
import org.jeecg.modules.operations.assistant.work.service.ICustomerInfoService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 示例不带参定时任务
 *
 * @author Scott
 */
@Slf4j
public class CompanyInfoJob implements Job {

    @Autowired
    private ICustomerInfoService customerInfoService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format(" Jeecg-Boot 普通定时任务 CompanyInfoJob !  时间:" + DateUtils.getTimestamp()));
        String serviceUrl = "https://www.gzxcbd.cn/StandardApiAction_login.action?account=shglxt&password=shxt12345";
        try {
            String res = ApiServiceUtil.get(serviceUrl);
            System.out.println("---------res-----" + res);
            JSONObject jsonObject = JSONObject.parseObject(res);
            Integer result = jsonObject.getInteger("result");
            if (result == 0) {
                System.out.println("---------result-----" + result);
                String jsession = jsonObject.getString("jsession");
                System.out.println("---------jsession-----" + jsession);
                serviceUrl = "https://www.gzxcbd.cn/StandardApiAction_findCompany.action?jsession=" + jsession;
                res = ApiServiceUtil.get(serviceUrl);
                jsonObject = JSONObject.parseObject(res);
                result = jsonObject.getInteger("result");
                if (result == 0) {
                    JSONArray vehicles = jsonObject.getJSONArray("infos");
                    Integer maxId = customerInfoService.findMaxTripartiteSystemId();
                    for (Object object : vehicles) {
                        try {
                            jsonObject = JSONObject.parseObject(object.toString());
                            if (jsonObject.getInteger("id") > maxId) {
                                CustomerInfo vehicleInfo = new CustomerInfo();
                                vehicleInfo.setTripartiteSystemId(jsonObject.getInteger("id"));
                                vehicleInfo.setCustomerName(jsonObject.getString("name"));
                                vehicleInfo.setContacts(jsonObject.getString("linkMan"));
                                vehicleInfo.setContactNumber(jsonObject.getString("linkPhone"));
                                vehicleInfo.setAddress(jsonObject.getString("legalAddress"));
                                customerInfoService.save(vehicleInfo);
                            }
                        } catch (Exception e) {
                            log.error("保存公司信息失败");
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("CompanyInfoJobException");
        }
    }
}